package controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import models.Resource;
import models.Setting;

import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import services.ConfigurationService;
import services.ProjectService;
import services.SettingService;
import services.TestCaseService;
import services.TestSuiteService;
import views.html.testcases.*;

public class TestCases extends Controller {
	
	private static final SettingService settingService = SettingService.getService();
	private static final ProjectService projectService = ProjectService.getService();
	private static final ConfigurationService configurationService = ConfigurationService.getService();
	private static final TestSuiteService testSuiteService = TestSuiteService.getService();
	private static final TestCaseService testCaseService = TestCaseService.getService();
	
	public static Result listTestCases(long projectId, long testSuiteId) {
		List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId);
		return ok(list.render(projectId, testSuiteId, testCases));
	}
	
	public static Result addTestCase(long projectId, long testSuiteId) {
		Form<Resource> form = Form.form(Resource.class);
		List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId);
		return ok(add.render(form, projectId, testSuiteId, testCases));
	}
	
	public static Result editTestCase(long projectId, long testSuiteId, long testCaseId) {
		Resource testCase = testCaseService.getTestCase(testCaseId);
		if (testCase != null) {
			Form<Resource> form = Form.form(Resource.class).fill(testCase);
			List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId);
			return ok(edit.render(testCaseId, form, projectId, testSuiteId, testCases));
		} else {
			return redirect(controllers.routes.TestCases.listTestCases(projectId, testSuiteId));
		}
	}
	
	public static Result createTestCase(long projectId, long testSuiteId) {
		Resource testCase = Form.form(Resource.class).bindFromRequest().get();
		// FilePart testCaseFile = upload();
		testCaseService.saveTestCase(projectId, testCase);
		return redirect(controllers.routes.TestCases.editTestCase(projectId, testSuiteId, testCase.getId()));
	}
	
	public static Result updateTestCase(long projectId, long testSuiteId, long testCaseId) {
		Resource testCase = testCaseService.getTestCase(testCaseId);
		if (testCase != null) {
			Resource newTestCase = Form.form(Resource.class).bindFromRequest().get();
			testCase.setName(newTestCase.getName());
			testCase.setDescription(newTestCase.getDescription());
			// FilePart testCaseFile = upload();
			testCaseService.saveTestCase(projectId, testCase);
			return redirect(controllers.routes.TestCases.editTestCase(projectId, testSuiteId, testCaseId));
		} else {
			return redirect(controllers.routes.TestCases.listTestCases(projectId, testSuiteId));
		}
	}
	
	// private static FilePart upload() {
	// MultipartFormData body = request().body().asMultipartFormData();
	// FilePart file = body.getFile("file");
	// return file;
	// }
	
	public static Result runTestWithDefaultConfig(long projectId, long testSuiteId, long testCaseId) {
		Resource defaultConfig = projectService.getDefaultConfig(projectId);
		return runTestWithConfig(projectId, testSuiteId, testCaseId, defaultConfig);
	}
	
	public static Result runTestWithConfig(long projectId, long testSuiteId, long testCaseId, long configId) {
		Resource config = configurationService.getConfig(configId);
		return runTestWithConfig(projectId, testSuiteId, testCaseId, config);
	}
	
	private static Result runTestWithConfig(long projectId, long testSuiteId, long testCaseId, Resource config) {
		Resource project = projectService.getProject(projectId);
		Resource testSuite = testSuiteService.getTestSuite(testSuiteId);
		Resource testCase = testCaseService.getTestCase(testCaseId);
		if (project == null || testSuite == null || testCase == null || config == null) {
			return redirect(controllers.routes.TestCases.listTestCases(projectId, testSuiteId));
		}
		String content = "";
		try {
			String testSuiteName = testSuite.getName();
			if (testSuiteName.endsWith(TestSuites.TEST_SUITE_FILE_EXTENSION)) {
				testSuiteName = testSuiteName.substring(0, testSuiteName.length() - TestSuites.TEST_SUITE_FILE_EXTENSION.length());
			}
			String testCaseName = testCase.getName().replace(" ", "");
			URL url = new URL(settingService.getSetting(Setting.TEST_EXECUTION_URL).getStringValue() + "/cgi-bin/console/execute.py?projectid=" + project.getName() + "&suite=" + testSuiteName + "&tcase=" + testCaseName + "&variablefile=" + config.getName());
			
			URLConnection connection = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				content += line + "\n";
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ok(content);
	}
	
}
